package com.fl.nj.service.impl;

import com.fl.nj.entity.Device;
import com.fl.nj.mapper.DeviceMapper;
import com.fl.nj.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 设备Service实现类
 * 实现设备的业务逻辑
 */
@Service
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    @Transactional
    public void save(Device device) {
        // 验证设备名称是否已存在
        Device existingDevice = deviceMapper.getByDeviceName(device.getDeviceName());
        if (existingDevice != null) {
            throw new RuntimeException("设备名称已存在: " + device.getDeviceName());
        }
        
        // 验证必填字段
        validateDevice(device);
        
        // 保存设备
        deviceMapper.save(device);
    }

    @Override
    @Transactional
    public void update(Device device) {
        // 验证设备是否存在
        Device existingDevice = deviceMapper.getById(device.getId());
        if (existingDevice == null) {
            throw new RuntimeException("设备不存在");
        }
        
        // 验证设备名称是否已被其他设备使用
        Device deviceWithSameName = deviceMapper.getByDeviceName(device.getDeviceName());
        if (deviceWithSameName != null && !deviceWithSameName.getId().equals(device.getId())) {
            throw new RuntimeException("设备名称已存在: " + device.getDeviceName());
        }
        
        // 验证必填字段
        validateDevice(device);
        
        // 更新设备
        deviceMapper.update(device);
    }

    @Override
    public Device getById(Long id) {
        return deviceMapper.getById(id);
    }

    @Override
    public Device getByDeviceName(String deviceName) {
        return deviceMapper.getByDeviceName(deviceName);
    }

    @Override
    public List<Device> getAll() {
        return deviceMapper.getAll();
    }
    
    @Override
    public List<Device> searchByDeviceName(String deviceName) {
        return deviceMapper.searchByDeviceName(deviceName);
    }

    @Override
    public List<Device> getByAreaId(Long areaId) {
        return deviceMapper.getByAreaId(areaId);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        // 验证设备是否存在
        Device device = deviceMapper.getById(id);
        if (device == null) {
            throw new RuntimeException("设备不存在");
        }
        
        // 验证设备下是否有数据库配置
        int databaseCount = deviceMapper.countDatabases(id);
        if (databaseCount > 0) {
            throw new RuntimeException("该设备下有数据库配置，无法删除");
        }
        
        // 删除设备
        deviceMapper.delete(id);
    }

    @Override
    public int countDatabases(Long deviceId) {
        return deviceMapper.countDatabases(deviceId);
    }

    /**
     * 验证设备的必填字段
     * @param device 设备对象
     */
    private void validateDevice(Device device) {
        if (device.getDeviceName() == null || device.getDeviceName().trim().isEmpty()) {
            throw new RuntimeException("设备名称不能为空");
        }
        if (device.getAreaId() == null) {
            throw new RuntimeException("所属区域不能为空");
        }
        if (device.getIpAddress() == null || device.getIpAddress().trim().isEmpty()) {
            throw new RuntimeException("IP地址不能为空");
        }
    }
}